﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace iPhoneTrackerWin.Data
{
    /// <summary>
    /// Manager zum Speichern von Daten mittels ORM
    /// </summary>
    public class GPXDataManager : AbstractDataManager
    {
        /// <summary>
        /// Stellt die GPX Datei dar
        /// </summary>
        File file
        {
            get;
            set;
        }

        /// <summary>
        /// Stellt den User dar
        /// </summary>
        User user
        {
            get;
            set;
        }

        public GPXDataManager(int UserID, int FileID)
        {
            var user = from u in DefaultContext.User
                       where u.ID == UserID
                       select u;
            User theUser = user.FirstOrDefault();

            var file = from f in DefaultContext.File
                       where f.ID == FileID
                       select f;
            File theFile = file.FirstOrDefault();

            init(theFile, theUser);
        }

        /// <summary>
        /// Erzeugt einen neuen GPX Daten-Manager für den angegebenen Nutzer und die angegebene Datei
        /// </summary>
        /// <param name="file"></param>
        /// <param name="user"></param>
        public GPXDataManager(File file, User user)
        {
            init(file, user);
        }

        private void init(File file, User user)
        {
            this.file = file;
            this.user = user;
        }

        #region add Methoden 

        /// <summary>
        /// Fügt einen neuen GPS Point zum Datenbank Kontext hinzu
        /// </summary>
        /// <param name="point"></param>
        protected void addGPSPoint(GPS point)
        {
            point.File = file;
            point.User = user;
            DefaultContext.AddToGPS(point);
        }

        /// <summary>
        /// Fügt ein neues TrackSegment zum Datenbank Kontext hinzu
        /// </summary>
        protected void addTrackSegment(TrackSegment trackSeg)
        {
            foreach (GPS p in trackSeg.GPS)
            {
                addGPSPoint(p);
            }
            DefaultContext.AddToTrackSegment(trackSeg);
        }

        /// <summary>
        /// Fügt einen neuen Track zum Datenbank Kontext hinzu
        /// </summary>
        /// <param name="track"></param>
        protected void addTrack(Track track)
        {
            track.File = file;
            foreach (TrackSegment ts in track.TrackSegment)
            {
                addTrackSegment(ts);
            }
            DefaultContext.AddToTrack(track);
        }

        /// <summary>
        /// Fügt eine neue Route zum Datenbank Kontext hinzu
        /// </summary>
        /// <param name="route"></param>
        protected void addRoute(Route route)
        {
            route.File = file;
            foreach (GPS p in route.GPS)
            {
                addGPSPoint(p);
            }
            DefaultContext.AddToRoute(route);
        }

        #endregion

        #region save Methoden

        /// <summary>
        /// Speichert die angegebenen Tracks
        /// </summary>
        /// <param name="tracks"></param>
        public void saveTracks(List<Track> tracks)
        {
            //Füge alle Tracks hinzu
            foreach (Track t in tracks)
                addTrack(t);

            //Speicher es
            DefaultContext.SaveChanges();
        }

        /// <summary>
        /// Speichert die angegebenen Routen
        /// </summary>
        /// <param name="routes"></param>
        public void saveRoutes(List<Route> routes)
        {
            //Füge alle Routen hinzu
            foreach (Route r in routes)
                addRoute(r);

            //Speicher es
            DefaultContext.SaveChanges();
        }

        /// <summary>
        /// Speichert die angegebenen Punkte
        /// </summary>
        /// <param name="points"></param>
        public void savePoints(List<GPS> points)
        {
            //Füge alle Punkte hinzu
            foreach (GPS p in points)
                addGPSPoint(p);

            //Speicher es
            DefaultContext.SaveChanges();
        }

        #endregion

    }
}
